package com.manage.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.manage.common.config.RuoYiConfig;
import com.manage.common.core.domain.entity.SysUser;
import com.manage.framework.config.ServerConfig;
import com.manage.modules.domain.TBaseServicePhoto;
import com.manage.modules.service.ITBaseServicePhotoService;
import com.manage.modules.utils.UploadUtil;
import com.manage.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.manage.modules.bo.TBaseServiceAddBo;
import com.manage.modules.bo.TBaseServiceQueryBo;
import com.manage.modules.bo.TBaseServiceEditBo;
import com.manage.modules.domain.TBaseService;
import com.manage.modules.mapper.TBaseServiceMapper;
import com.manage.modules.vo.TBaseServiceVo;
import com.manage.modules.service.ITBaseServiceService;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 维护单管理Service业务层处理
 *
 * @author wx
 * @date 2021-08-26
 */
@Service
public class TBaseServiceServiceImpl extends ServiceImpl<TBaseServiceMapper, TBaseService> implements ITBaseServiceService {

    @Resource
    private ITBaseServicePhotoService servicePhotoService;
    @Resource
    private ServerConfig serverConfig;
    @Resource
    private ISysUserService sysUserService;

    @Override
    public TBaseServiceVo queryById(Long serviceId) {
        TBaseService db = this.baseMapper.selectById(serviceId);
        // 查询图片
        List<TBaseServicePhoto> photoList = servicePhotoService.findByServiceId(db.getServiceId());
        // 加上路径
        for (TBaseServicePhoto servicePhoto : photoList) {
            servicePhoto.setUrl(serverConfig.getUrl() + servicePhoto.getUrl());
            servicePhoto.setLocal(RuoYiConfig.getProfile() + servicePhoto.getLocal());
        }
        TBaseServiceVo baseServiceVo = BeanUtil.toBean(db, TBaseServiceVo.class);
        baseServiceVo.setTBaseServicePhotoList(photoList);
        List<SysUser> userList = sysUserService.listIdAndNickname();
        baseServiceVo.setServicePeopleName(sysUserService.getNickNameByUserId(baseServiceVo.getServiceUserId(), userList));
        return baseServiceVo;
    }

    @Override
    public List<TBaseServiceVo> queryList(TBaseServiceQueryBo bo) {
        LambdaQueryWrapper<TBaseService> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getStationIdC()), TBaseService::getStationIdC, bo.getStationIdC());
        lqw.eq(bo.getEquipHistoryId() != null, TBaseService::getEquipHistoryId, bo.getEquipHistoryId());
        lqw.like(StrUtil.isNotBlank(bo.getServiceCode()), TBaseService::getServiceCode, bo.getServiceCode());
        lqw.ge(bo.getServiceStartTime() != null, TBaseService::getServiceStartTime, bo.getServiceStartTime());
        lqw.le(bo.getServiceEndTime() != null, TBaseService::getServiceEndTime, bo.getServiceEndTime());
        lqw.eq(bo.getServiceTypeId() != null, TBaseService::getServiceTypeId, bo.getServiceTypeId());
        lqw.eq(bo.getServiceState() != null, TBaseService::getServiceState, bo.getServiceState());

        List<TBaseServiceVo> voList = entity2Vo(this.list(lqw));
        List<SysUser> userList = sysUserService.listIdAndNickname();
        // 设置维护人
        for (TBaseServiceVo serviceVo : voList) {
            serviceVo.setServicePeopleName(sysUserService.getNickNameByUserId(serviceVo.getServiceUserId(), userList));
        }
        return voList;
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<TBaseServiceVo> entity2Vo(Collection<TBaseService> collection) {
        List<TBaseServiceVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TBaseServiceVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TBaseService> page = (Page<TBaseService>) collection;
            Page<TBaseServiceVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TBaseServiceAddBo bo) {
        TBaseService add = BeanUtil.toBean(bo, TBaseService.class);
        validEntityBeforeSave(add);
        this.save(add);

        List<TBaseServicePhoto> tBaseServicePhotoList = bo.getSitePhotoUrlList();
        // 只存相对路径
        setUrl(tBaseServicePhotoList, add.getServiceId());
        servicePhotoService.saveOrUpdateBatch(tBaseServicePhotoList);

        // 删除照片
        servicePhotoService.delete(bo.getDeletePhotoList());
        return true;
    }

    @Override
    public Boolean updateByEditBo(TBaseServiceEditBo bo) {
        TBaseService update = BeanUtil.toBean(bo, TBaseService.class);
        validEntityBeforeSave(update);

        List<TBaseServicePhoto> tBaseServicePhotoList = bo.getSitePhotoUrlList();
        // 只存相对路径
        setUrl(tBaseServicePhotoList, update.getServiceId());
        servicePhotoService.saveOrUpdateBatch(tBaseServicePhotoList);

        // 删除照片
        servicePhotoService.delete(bo.getDeletePhotoList());
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TBaseService entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }// 先删除照片
        servicePhotoService.deleteByServiceIds(ids);
        return this.removeByIds(ids);
    }

    /**
     * 路径修正
     */
    private void setUrl(List<TBaseServicePhoto> tBaseServicePhotoList, Long serviceId) {
        // 只存相对路径
        for (TBaseServicePhoto tBaseServicePhoto : tBaseServicePhotoList) {
            tBaseServicePhoto.setServiceId(serviceId);
            tBaseServicePhoto.setUrl(UploadUtil.getUrlByDelete(tBaseServicePhoto.getUrl(), serverConfig.getUrl()));
            tBaseServicePhoto.setLocal(UploadUtil.getUrlByDelete(tBaseServicePhoto.getLocal(), RuoYiConfig.getProfile()));
        }
    }
}
